#include "curvewidget.h"

CurveWidget::CurveWidget(QGraphicsWidget* parent)
    : QGraphicsWidget(parent)
{
    setAcceptDrops(true);
}

CurveWidget::CurveWidget(QSize size)
{
    m_size = size;
    setAcceptDrops(true);
}

void CurveWidget::setPainterPath(QPainterPath *pPainterPath)
{
    m_pPainterPath = pPainterPath;
}


void CurveWidget::setColors(QColor backgroundColor, QColor foregroundColor)
{
    m_backgroundColor = backgroundColor;
    m_foregroundColor = foregroundColor;
}

void CurveWidget::setLineWidth(int width)
{
    m_lineWidth = width;
}


// 绘制图形
void CurveWidget::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    Q_UNUSED(widget)
    Q_UNUSED(option)

    QPixmap pixmap(size().toSize());
    // 填充背景色并绘制背景
    pixmap.fill(m_backgroundColor);
    QPainter innerPainter(&pixmap);

    // 设置画笔并绘制曲线
    QPen pen(m_foregroundColor);
    pen.setWidth(m_lineWidth);
    innerPainter.setPen(pen);
    innerPainter.drawPath(*m_pPainterPath);

    painter->drawPixmap(0, 0, pixmap);

}

QRectF CurveWidget::boundingRect() const
{
    return QRectF(-m_size.width() / 2, -m_size.height() / 2, m_size.width(), m_size.height());
}
